package com.turman.coroutionsample.view

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.turman.coroutionsample.ui.theme.CoroutionSampleTheme
import com.turman.coroutionsample.viewmodel.GithubViewModel

@Composable
fun RetrofitFlowPage() {
    var inputString by remember {
        mutableStateOf("")
    }
    val viewModel = viewModel<GithubViewModel>()
    val list = viewModel.searchGithubProjects(inputString).collectAsState(null)
    CoroutionSampleTheme {
        Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
            Column {
                TextField(
                    value = inputString,
                    onValueChange = { inputString = it },
                    modifier = Modifier.fillMaxWidth()
                )
                Spacer(modifier = Modifier.height(10.dp))
                LazyColumn{
                    items(list?.value?.size?:0){
                        Text(text = list?.value?.get(it)?.full_name ?: "")
                    }
                }
            }
        }
    }
}